var app = getApp()
Page({
  data: {
   status:1,
   isShowModal:true,
   hours: '00',
   minutes: '00',
   seconds: '00'
  },
  onReady:function(){
    var that = this;
  },
  onLoad: function (e) {
    var that = this
    if (e && e.enterfrom) {
      var enterfrom = e.enterfrom == 'share' ? true : false;
      that.setData({
        enterfrom: enterfrom
      })
    }
    var tid = e.tid;
    console.log(tid)
    that.resultData(tid);
    that.setData({
      // userInfo: app.globalData.userInfo
      tid:tid
    })
  },
  resultData: function (tid){
    var that = this;
    console.log(tid)
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: app.globalData.requestUrl,
      data: {
        map: 'applet_mall_group_join',
        tid:tid
      },
      success: function (res) {
        if (res.data.ec == 200) {
          console.log(res.data.data);
          that.time(res.data.data.over_time)
          that.djs(res.data.data.over_time);
          that.setData({
            resultData: res.data.data,
            tapTime: res.data.data.over_time,
            share: res.data.data.share
          })
          // that.djs();
          // app.errorTip(that, res.data.data.msg, 2000);
        } else {
          console.log(res.data.em);
          app.errorTip(that, res.data.em, 2000);
        }
      },
      complete: function () {
        wx.hideLoading();
      }
    });
  },
  // makeCall: function () {
  //   wx.showModal({
  //     title: '',
  //     content: '联系商家客服 0371-86258375?',
  //     success: function (res) {
  //       if (res.confirm) {
  //         wx.makePhoneCall({
  //           phoneNumber: '0371-86258375',
  //           success: function () {
  //             console.log("拨打电话成功！")
  //           },
  //           fail: function () {
  //             console.log("拨打电话失败！")

  //           }
  //         })
  //       }
  //     }
  //   })
  // },
  hideModal: function () {
    var that = this;
    that.setData({
      isShowModal: false
    })
  },
  djs:function(time){
    var that = this;
    var leftTime = time;
    this.data.intervarID = setInterval(function () {
      // var leftTime = (new Date(tapTime)) - (new Date()); //计算剩余的毫秒数 
      // console.log(leftTime);
      leftTime--;
      that.time(leftTime)
    }, 1000)
  },
  time: function (leftTime){
    var that= this;
    if (leftTime > 0) {
      // var days = parseInt(leftTime / 1000 / 60 / 60 / 24, 10); //计算剩余的天数 
      var hours = parseInt(leftTime / 60 / 60, 10); //计算剩余的小时 
      var minutes = parseInt(leftTime / 60 % 60, 10);//计算剩余的分钟 
      var seconds = parseInt(leftTime % 60, 10);//计算剩余的秒数 
      // days = days < 10 ? '0' + days : days;
      // console.log(days);
      hours = hours < 10 ? '0' + hours : hours;
      minutes = minutes < 10 ? '0' + minutes : minutes;
      seconds = seconds < 10 ? '0' + seconds : seconds;
      that.setData({
        hours: hours,
        minutes: minutes,
        seconds: seconds
      })
      if (hours == '00' && minutes == '00' && seconds == '00') {
        clearInterval(that.data.intervarID);
      }
    } else {
      that.setData({
        hours: '00',
        minutes: '00',
        seconds: '00'
      })
    } 
  },
  onShareAppMessage: function () {
    var that = this;
    var tid = that.data.tid;
    var gtitle = that.data.resultData.group_desc.title;
    var gcover = that.data.resultData.group.gcover;
    var title = that.data.resultData.share.title;
    var cover = that.data.resultData.share.img;
    title = title ? title : gtitle;
    cover = cover ? cover : gcover;
    app.getPoint(that);
    return {
      title: title,
      imageUrl: cover,
      path: '/subpages/joinGroup/joinGroup?enterfrom=share&tid='+tid
    }
  },
  backIndex:function(){
    wx.redirectTo({
      url:"/pages/groupIndex/groupIndex"
    })
  },
  inviteFriends:function(){
    var that = this;
    that.setData({
      isShowModal: true
    })
  }
})